package com.example.x6.configurationmaintenance.Activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.x6.configurationmaintenance.Interface.OnVersionControlGetListener;
import com.example.x6.configurationmaintenance.Module.UpdateManager;
import com.example.x6.configurationmaintenance.R;
import com.example.x6.configurationmaintenance.Service.DownloadService;
import com.example.x6.configurationmaintenance.Utils.VersionUtils;
import java.util.HashMap;

public class UpdateActivity extends Activity {
    private final String TAG = getClass().getSimpleName();
    private TextView localversionName;
    private TextView newVersionName;
    private ProgressDialog progressDialog;
    private TextView releaseMessage;
    private TextView status;
    private Button updateButton;
    private UpdateManager updateManager;

    /* access modifiers changed from: protected */
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        this.updateManager = new UpdateManager();
        this.progressDialog = new ProgressDialog(this);
        initView();
    }

    private void initView() {
        this.status = (TextView) findViewById(R.id.status);
        this.localversionName = (TextView) findViewById(R.id.localversionName);
        TextView textView = this.localversionName;
        setText(textView, getResources().getString(R.string.current_version) + VersionUtils.getVerName(this));
        this.newVersionName = (TextView) findViewById(R.id.newVersionName);
        this.releaseMessage = (TextView) findViewById(R.id.releaseMessage);
        this.updateButton = (Button) findViewById(R.id.updateButton);
        showDialog(null);
        this.updateManager.setOnVersionControlGetListener(new OnVersionControlGetListener() {
            /* class com.example.x6.configurationmaintenance.Activity.UpdateActivity.AnonymousClass1 */

            @Override // com.example.x6.configurationmaintenance.Interface.OnVersionControlGetListener
            public void onVersionContolGet(final HashMap<String, String> hashMap) {
                int versionCode;
                UpdateActivity.this.dismissDialog();
                UpdateActivity.this.isDismissTextView(UpdateActivity.this.newVersionName, true);
                UpdateActivity.this.isDismissTextView(UpdateActivity.this.releaseMessage, true);
                UpdateActivity.this.isDismissButton(UpdateActivity.this.updateButton, true);
                if (hashMap == null) {
                    UpdateActivity.this.setText(UpdateActivity.this.status, UpdateActivity.this.getResources().getString(R.string.get_version_failed));
                    return;
                }
                try {
                    versionCode = Integer.parseInt(hashMap.get(UpdateManager.versionCode));
                } catch (Exception e) {
                    versionCode = -1;
                }
                if (versionCode == -1 || versionCode <= VersionUtils.getVersionCode(UpdateActivity.this)) {
                    UpdateActivity.this.setText(UpdateActivity.this.status, UpdateActivity.this.getResources().getString(R.string.new_verison_already));
                    return;
                }
                UpdateActivity.this.setText(UpdateActivity.this.newVersionName, UpdateActivity.this.getResources().getString(R.string.new_version) + hashMap.get(UpdateManager.version));
                String showReleaseMessage = UpdateActivity.this.getResources().getString(R.string.version_update) + "\n";
                for (String string : hashMap.get(UpdateManager.releaseMessage).split("\\s+")) {
                    showReleaseMessage = showReleaseMessage + string + "\n";
                }
                UpdateActivity.this.setText(UpdateActivity.this.releaseMessage, showReleaseMessage);
                UpdateActivity.this.isDismissTextView(UpdateActivity.this.newVersionName, false);
                UpdateActivity.this.isDismissTextView(UpdateActivity.this.releaseMessage, false);
                UpdateActivity.this.updateButton.setOnClickListener(new View.OnClickListener() {
                    /* class com.example.x6.configurationmaintenance.Activity.UpdateActivity.AnonymousClass1.AnonymousClass1 */

                    public void onClick(View view) {
                        if (!DownloadService.isDownload()) {
                            Intent intent = new Intent(UpdateActivity.this, DownloadService.class);
                            intent.putExtra(DownloadService.fileurlKey, (String) hashMap.get(UpdateManager.url));
                            intent.putExtra(DownloadService.md5Key, (String) hashMap.get(UpdateManager.md5));
                            UpdateActivity.this.startService(intent);
                            return;
                        }
                        Log.w(UpdateActivity.this.TAG, "正在下载");
                    }
                });
                UpdateActivity.this.isDismissButton(UpdateActivity.this.updateButton, false);
            }
        });
    }

    private void showDialog(final String message) {
        runOnUiThread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Activity.UpdateActivity.AnonymousClass2 */

            public void run() {
                UpdateActivity.this.progressDialog.setTitle(UpdateActivity.this.getResources().getString(R.string.tips));
                if (message == null) {
                    UpdateActivity.this.progressDialog.setMessage(UpdateActivity.this.getResources().getString(R.string.geting_version));
                } else {
                    UpdateActivity.this.progressDialog.setMessage(message);
                }
                UpdateActivity.this.progressDialog.show();
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void dismissDialog() {
        runOnUiThread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Activity.UpdateActivity.AnonymousClass3 */

            public void run() {
                UpdateActivity.this.progressDialog.dismiss();
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void isDismissTextView(final TextView textView, final boolean isDismiss) {
        if (textView != null) {
            runOnUiThread(new Runnable() {
                /* class com.example.x6.configurationmaintenance.Activity.UpdateActivity.AnonymousClass4 */

                public void run() {
                    if (isDismiss) {
                        textView.setVisibility(8);
                    } else {
                        textView.setVisibility(0);
                    }
                }
            });
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void isDismissButton(final Button button, final boolean isDismiss) {
        if (button != null) {
            runOnUiThread(new Runnable() {
                /* class com.example.x6.configurationmaintenance.Activity.UpdateActivity.AnonymousClass5 */

                public void run() {
                    if (isDismiss) {
                        button.setVisibility(8);
                    } else {
                        button.setVisibility(0);
                    }
                }
            });
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void setText(final TextView textView, final String text) {
        if (textView != null && text != null) {
            runOnUiThread(new Runnable() {
                /* class com.example.x6.configurationmaintenance.Activity.UpdateActivity.AnonymousClass6 */

                public void run() {
                    textView.setText(text);
                }
            });
        }
    }
}
